package com.yxh.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.os.Looper;
import android.widget.Toast;

import com.yxh.ZybApplication;
import com.yxh.service.MainService;


/**
 * 异常类log捕获
 * 
 * @author luoming
 */
public class CrashUtil implements UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private static CrashUtil mInstance;

    private CrashUtil() {
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    public void startWork() {
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public static CrashUtil getInstance() {
        if (mInstance == null) {
            mInstance = new CrashUtil();
        }
        return mInstance;
    }

    public static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
    	
    	 LogUtil.e("崩溃异常:"
                 + String.format("At %s Thread(%s#%d) got uncaught Exception: %s", DateUtil.getCurrentTime(),
                         thread.getName(), thread.getId(), getStackTrace(ex)));
    	
//        if (!handleException(ex) && mDefaultHandler != null) {
//            //如果用户没有处理则让系统默认的异常处理器来处理    
//            mDefaultHandler.uncaughtException(thread, ex);
//        } else {
//            LogUtil.e("崩溃异常:"
//                    + String.format("At %s Thread(%s#%d) got uncaught Exception: %s", DateUtil.getCurrentTime(),
//                            thread.getName(), thread.getId(), getStackTrace(ex)));
//            try {
//                Thread.sleep(500L);
//                Intent intent = new Intent(ZybApplication.getContext(), MainActivity.class);
//                PendingIntent restartIntent = PendingIntent.getActivity(ZybApplication.getContext(), 0, intent,
//                        Intent.FLAG_ACTIVITY_NEW_TASK);
//                //退出程序                                          
//                AlarmManager mgr = (AlarmManager) ZybApplication.getContext().getSystemService(Context.ALARM_SERVICE);
//                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent); // 1秒钟后重启应用   
//                finishActivity();
//            } catch (Exception e) {
//
//            }
//        }
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     * 
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false.
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }
        //使用Toast来显示异常信息    
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(ZybApplication.getContext(), "很抱歉,程序出现异常,即将重启.", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }.start();
        return true;
    }

    public void finishActivity() {
        for (Activity activity : MainService.allActivity) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程  
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
